﻿using Coldairarrow.Entity.MEGA;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.MEGA
{
    public class MEGA_LeafBladeAndWorkerBusiness : BaseBusiness<MEGA_LeafBladeAndWorker>, IMEGA_LeafBladeAndWorkerBusiness, ITransientDependency
    {
        public MEGA_LeafBladeAndWorkerBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<MEGA_LeafBladeAndWorkerDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<MEGA_LeafBladeAndWorker>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<MEGA_LeafBladeAndWorker, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
            q = q.Where(where);
            var qq = from a in q
                     join b in Db.GetIQueryable<MEGA_Worker>() on a.WId equals b.Id
                     select new MEGA_LeafBladeAndWorkerDTO
                     {
                         Code = b.Code,
                         Id = a.Id,
                         CreateTime = a.CreateTime,
                         CreatorId = a.CreatorId,
                         Image = b.Image,
                         JoinTime = b.JoinTime,
                         LBId = a.LBId,
                         Name = b.Name,
                         Note = b.Note,
                         Phone = b.Phone,
                         Sex = b.Sex,
                         WId = a.WId

                     };
            return await qq.GetPageResultAsync(input);
        }

        public async Task<MEGA_LeafBladeAndWorker> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(MEGA_LeafBladeAndWorker data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(MEGA_LeafBladeAndWorker data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}